package com.sphd.emanu.activity.storeroom;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.materials.MaterialsActivity;
import com.sphd.emanu.activity.storagemanage.StorageManageActivity;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.aterials.AterialsFragment;
import com.sphd.emanu.fragment.goods.GoodsFragment;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.popupwindow.MorePopWindow;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 库房
 *
 * @Author Hu
 * @Time 2017/5/25 -- 12:05.
 * @Email 87683202@qq.com
 */
public class StoreroomActivity extends MVPBaseActivity<StoreroomContract.View, StoreroomPresenter> implements StoreroomContract.View,
        MorePopWindow.PopWinOnClickListener {
    @BindView(R.id.aterials)
    TextView aterials;
    @BindView(R.id.goods)
    TextView goods;
    @BindView(R.id.btn_more)
    TextView btnMore;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    int touchId = -1;
    AterialsFragment aterialsFragment;
    GoodsFragment goodsFragment;
    Intent intent;
    Bundle bundle;
    User user;
    boolean isCanDo=false;
    List<Moudle> moudleList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_storeroom);
        ButterKnife.bind(this);
        intiView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void intiView() {
        setTitleBar(toolbar, "");
        intent = getIntent();
        bundle = intent.getExtras();
        user= (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudleList= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudleList, PermissionConfig.MENU_STOREHOUSE_8);
        onViewClicked(aterials);
    }

    @OnClick({R.id.aterials, R.id.goods, R.id.btn_more})
    public void onViewClicked(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideAll(fragmentTransaction);
        switch (view.getId()) {
            case R.id.aterials:
                if (touchId == 0) {
                    return;
                } else {
                    aterials.setTextColor(ContextCompat.getColor(context, R.color.white));
                    aterials.setTextSize(14);
                    goods.setTextColor(ContextCompat.getColor(context, R.color.whitesmoke));
                    goods.setTextSize(10);
                    touchId = 0;
                    if (aterialsFragment == null) {
                        aterialsFragment = new AterialsFragment();
                        fragmentTransaction.add(R.id.my_fragment, aterialsFragment);
                        aterialsFragment.setArguments(bundle);
                    } else {
                        fragmentTransaction.show(aterialsFragment);
                    }
                    fragmentTransaction.commit();

                }
                break;
            case R.id.goods:
                if (touchId == 1) {
                    return;
                } else {
                    goods.setTextColor(ContextCompat.getColor(context, R.color.white));
                    goods.setTextSize(14);
                    aterials.setTextColor(ContextCompat.getColor(context, R.color.whitesmoke));
                    aterials.setTextSize(10);
                    touchId = 1;
                    if (goodsFragment == null) {
                        goodsFragment = new GoodsFragment();
                        goodsFragment.setArguments(bundle);
                        fragmentTransaction.add(R.id.my_fragment, goodsFragment);
                    } else {
                        fragmentTransaction.show(goodsFragment);
                    }
                    fragmentTransaction.commit();
                }
                break;
            case R.id.btn_more:
                MorePopWindow morePopWindow = new MorePopWindow(this);
                morePopWindow.showPopupWindow(btnMore);
                morePopWindow.setPopWinOnClickListener(this);
                break;
        }

    }

    private void hideAll(FragmentTransaction fragmentTransaction) {
        if (aterialsFragment != null) {
            fragmentTransaction.hide(aterialsFragment);
        }
        if (goodsFragment != null) {
            fragmentTransaction.hide(goodsFragment);
        }

    }


    @Override
    public void popWinOnClick(View view) {
        switch (view.getId()) {
            case R.id.add_category:
                if (isCanDo){
                    View viewDialog = View.inflate(context, R.layout.dialog_edit, null);
                    final MaterialEditText myEdit = (MaterialEditText) viewDialog.findViewById(R.id.my_edit);
                    final AlertDialog alertDialog = getDialogBuilder(viewDialog, "新增分类", "确定", "取消");
                    alertDialog.show();
                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (TextUtils.isEmpty(myEdit.getText().toString().trim())) {
                                toast("分类不能为空");
                            } else {
                                mPresenter.addMaterialCategory(1,myEdit.getText().toString().trim(),user.getOid(),alertDialog);
                            }
                        }
                    });
                }else {
                    toast(getString(R.string.no_perssion));
                }


                break;
            case R.id.matilr_stroage:
                intent.setClass(context, StorageManageActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void setAddRes(int res, AlertDialog viewDialog) {
        if (res==1){
            toast("操作成功");
            viewDialog.dismiss();
        }else {
            toast("操作失败，请重试。");
        }
    }
}
